﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;

/// <summary>
/// 数据库配置
/// </summary>
public class DBConfig
{
    /// <summary>
    /// 连接字符串
    /// </summary>
    private static string CONNECT_STRING = @"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=" + HttpRuntime.AppDomainAppPath + @"\App_Data\weiboDB.accdb";

    /// <summary>
    /// 创建一个连接
    /// </summary>
    /// <returns>数据库连接</returns>
    public static DbConnection CreateConnect()
    {
        DbConnection result = Factory.CreateConnection();
        result.ConnectionString = CONNECT_STRING;
        return result;
    }

    /// <summary>
    /// 创建数据库命令
    /// </summary>
    /// <param name="commandText">命令字符串</param>
    /// <returns>数据库命令</returns>
    public static DbCommand CreateCommand()
    {
        DbCommand result = Factory.CreateCommand();
        return result;
    }

    /// <summary>
    /// 创建数据库命令
    /// </summary>
    /// <param name="commandText">命令字符串</param>
    /// <returns>数据库命令</returns>
    public static DbCommand CreateCommand(string commandText)
    {
        DbCommand result = Factory.CreateCommand();
        if (commandText != null)
            result.CommandText = commandText;
        return result;
    }

    /// <summary>
    /// 创建数据库参数
    /// </summary>
    /// <param name="name">参数名称</param>
    /// <param name="value">参数值</param>
    /// <returns>数据库参数</returns>
    public static DbParameter CreateParameter(string name, object value)
    {
        DbParameter result = Factory.CreateParameter();
        result.ParameterName = name;
        result.Value = value;
        return result;
    }

    /// <summary>
    /// 创建数据库参数
    /// </summary>
    /// <param name="name">参数名称</param>
    /// <param name="value">参数值</param>
    /// <returns>数据库参数</returns>
    public static DbParameter CreateParameter(string name, object value, OleDbType type)
    {
        DbParameter result = Factory.CreateParameter();
        result.ParameterName = name;
        result.Value = value;
        if (result is OleDbParameter)
            (result as OleDbParameter).OleDbType = type;
        return result;
    }

    public static DbDataAdapter CreateDataAdapter()
    {
        return Factory.CreateDataAdapter();
    }

    /// <summary>
    /// 数据库对象工厂
    /// 要操作不同的数据库，请修改此方法的返回值
    /// </summary>
    public static DbProviderFactory Factory
    {
        get
        {
            return OleDbFactory.Instance;
        }
    }

}